package sanphone.com.runningschool.MyMessage;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import sanphone.com.runningschool.BaseActivity;
import sanphone.com.runningschool.ForceOffline;
import sanphone.com.runningschool.InterfaceLocation;
import sanphone.com.runningschool.R;
import sanphone.com.runningschool.RequestHttp.RequestHttp;
import sanphone.com.runningschool.entity.Question;

public class MyMessageActivity extends BaseActivity {

    private String url = InterfaceLocation.getLocation() + "getUserAllHelp?schoolId=";
    private Toolbar toolbar;
    private ListView messageList;
    private List<Question> questions;
    private String id;
    private MyMessageAdapter adapter;
    private TextView message_empty;
    private SwipeRefreshLayout message_refresh;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_message);
        SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);
        initView();
        id = pref.getString("schoolId", null);
        ForceOffline offline = new ForceOffline(id, this);
        offline.check();

    }

    @Override
    protected void onResume() {
        getDataFromInternet();
        super.onResume();
    }

    private void initView() {
        toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        messageList = findViewById(R.id.message_list);
        message_empty = findViewById(R.id.message_empty);
        message_refresh = findViewById(R.id.message_refresh);
        message_refresh.setColorSchemeResources(R.color.colorPrimary);
        message_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Messagerefresh();
            }
        });
        message_refresh.post(new Runnable() {
            @Override
            public void run() {
                message_refresh.setRefreshing(true);
                Messagerefresh();
            }
        });

    }

    private void getDataFromInternet() {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                RequestHttp requestHttp = new RequestHttp(url + id,MyMessageActivity.this);
                try {
                    String response = requestHttp.get();
                    String data = new JSONObject(response).getString("data");
                    Log.e("MyMessageActivity", data);
                    Gson gson = new Gson();
                    questions = gson.fromJson(data, new TypeToken<List<Question>>() {
                    }.getType());
                    Collections.reverse(questions);
                    if (questions.size() == 0) {
                        message_empty.setVisibility(View.VISIBLE);
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                //将setAdatper放到ui线程中，防止出现crash现象
                                adapter = new MyMessageAdapter(MyMessageActivity.this, questions);
                                messageList.setAdapter(adapter);
                            }

                        });
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        executorService.shutdown();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                MyMessageActivity.this.finish();
                break;
        }
        return true;
    }

    public void Messagerefresh() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        getDataFromInternet();
                        message_refresh.setRefreshing(false);
                    }
                });
            }
        }).start();
    }
}
